#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <string.h>
#define COMMANDCOUNT 100
char command[COMMANDCOUNT];
int main(int argc, char* argv[], char* env[])
{
    while(1)
    {
        //printf("[%s@%s myshell]$ ", getenv("USER"), getenv("HOSTNAME"));
        printf("[用户名@主机名 文件名]$ ");
        fflush(stdout);
        fgets(command, COMMANDCOUNT - 1, stdin);
        //把读入的\n换成\0
        command[strlen(command) - 1] = 0; 
        char* _argv[100];
        char* str = (char*)" ";
        _argv[0] = strtok(command, str);
        int i = 1;

        while(_argv[i++] = strtok(NULL, str));
        pid_t id = fork();
        if(id == 0)
        {
             execvp(_argv[0], _argv);
             exit(1);
        }
        else 
        {
            int status = 0;
            pid_t ret = waitpid(id, &status, 0); 
            if(WEXITSTATUS(status) == 1)
            {
                printf("input error\n");
            }
            (void)ret;
        }

    }
    return 0;
}
